import pygame, random
import ResourceManager, GameObject, EvilGreg, Kid, DeadKid #,BodyParts, Blood, Fire
from EvilGreg import *
from Kid import *
from ResourceManager import *
from DeadKid import *
from Spawner import KillBasedSpawner

class GameObjectManager(object):

    NUM_SOUNDS = 3
    (CRUNCH_SOUND,
     KID_DIE_SOUND,
     EG_EAT_SOUND) = range(NUM_SOUNDS)

    def __init__(self):
        self.evilGreg = EvilGreg()
        self.baseKid = Kid()
        self.baseDeadKid = DeadKid(DeadKid.RIGHT)
        self.kids = pygame.sprite.OrderedUpdates()
        self.deadKids = pygame.sprite.RenderPlain()

#        self.bodyParts = pygame.sprite.RenderPlain()
#        self.blood = pygame.sprite.RenderPlain()



    def linkAnimations(self, resourceManager, fps):
        self.evilGreg.linkAnimations(resourceManager,fps)
        self.baseKid.linkAnimations(resourceManager,fps)
        self.baseDeadKid.linkAnimations(resourceManager, fps)

    def linkSounds(self,resourceManager):
        self.evilGreg.linkSounds(resourceManager)
        self.baseKid.linkSounds(resourceManager)
        self.sounds = range(self.NUM_SOUNDS)
        self.sounds[self.CRUNCH_SOUND]  = resourceManager.getSound('Kid_Crunch')
        self.sounds[self.KID_DIE_SOUND] = resourceManager.getSound('Kid_Die')
        self.sounds[self.EG_EAT_SOUND]  = resourceManager.getSound('EG_Eat')

    def reset(self):
        self.evilGreg.reset()
        self.kids.empty()
        self.deadKids.empty()
        self.run = True
        self.spawner.reset()

    def setSpawner(self, spawner):
        self.spawner = spawner
        
    #update all the sprites, check for collisions and handle them, draw on the screen
    def update(self, screen):

        #add random kids on a timer
        self.spawner.spawn()
        
        self.deadKids.update()
        self.kids.update(self.evilGreg.rect)
        self.evilGreg.update()

        self.checkCollisions()


        self.deadKids.draw(screen)
        self.kids.draw(screen)
        screen.blit(self.evilGreg.image, self.evilGreg.rect)

         

    def checkCollisions(self):
        self.checkPunch()
        self.checkKick()
        self.checkKidAttack()
        self.checkEvilGregEat()
        self.checkEvilGregFlame()
        
    def checkPunch(self):
        if self.evilGreg.isActionActive(EvilGreg.PUNCH):
            fist = pygame.sprite.Sprite()
            if self.evilGreg.facing == GameObject.RIGHT:
                fist.rect = Rect(self.evilGreg.rect.left + 50, self.evilGreg.rect.top + 50, 10,10)
            else:
                fist.rect = Rect(self.evilGreg.rect.left, self.evilGreg.rect.top + 50, 10, 10)

            self.hitKids(pygame.sprite.spritecollide(fist,self.kids,False), EvilGreg.PUNCH_DAMAGE)


    def checkKick(self):
        if self.evilGreg.isActionActive(EvilGreg.KICK):
            foot = pygame.sprite.Sprite()
            if self.evilGreg.facing == GameObject.RIGHT:
                foot.rect = Rect(self.evilGreg.rect.left + 50, self.evilGreg.rect.top + 60, 10, 20)
            else:
                foot.rect = Rect(self.evilGreg.rect.left, self.evilGreg.rect.top + 60, 10, 20)
            
            self.hitKids(pygame.sprite.spritecollide(foot,self.kids,False), EvilGreg.KICK_DAMAGE)

        
    def checkEvilGregFlame(self):
        if self.evilGreg.isActionActive(EvilGreg.FLAME):
            flame = pygame.sprite.Sprite()
            if self.evilGreg.facing == GameObject.RIGHT:
                flame.rect = Rect(self.evilGreg.rect.left + 50, self.evilGreg.rect.top + 30, 60, 60)
            else:
                flame.rect = Rect(self.evilGreg.rect.left, self.evilGreg.rect.top + 60, 30, 60)
            
            self.hitKids(pygame.sprite.spritecollide(flame,self.kids,False), EvilGreg.FLAME_DAMAGE)


    def hitKids(self, kidList, damage):
        for kid in kidList:
            self.sounds[self.CRUNCH_SOUND].play()
            
            kid.decHealth(damage)
            if not kid.isAlive():
                if kid.facing == Kid.LEFT: facing = DeadKid.LEFT
                else: facing = DeadKid.RIGHT
                self.addDeadKid(kid.rect.topleft,facing)
                self.evilGreg.kills += 1
                kid.kill()
                self.sounds[self.KID_DIE_SOUND].play()


    def checkEvilGregEat(self):
        if self.evilGreg.isActionActive(EvilGreg.EAT):
            mouth = pygame.sprite.Sprite()
            if self.evilGreg.facing == GameObject.RIGHT:
                mouth.rect = Rect(self.evilGreg.rect.left + 50, self.evilGreg.rect.top + 50, 10, 10)
            else:
                mouth.rect = Rect(self.evilGreg.rect.left, self.evilGreg.rect.top + 50, 10, 10)
            
            self.eatKids(pygame.sprite.spritecollide(mouth,self.deadKids,True))
        
    def eatKids(self, kidList):
        for kid in kidList:
            self.sounds[self.EG_EAT_SOUND].play()
            self.evilGreg.incHunger(EvilGreg.MEAL_HUNGER_BONUS)


    def checkKidAttack(self):
        for kid in self.kids:
            if kid.isActionActive(Kid.BITE):
                kid.sounds[Kid.BITE].play()
                self.evilGreg.decHealth(Kid.BITE_DAMAGE)



    def addDeadKid(self, position, facing):
        newDeadKid = DeadKid(facing)
        newDeadKid.animations = self.baseDeadKid.animations
        newDeadKid.setPosition(position[0]-10,position[1]+20)
        newDeadKid.currentAnimation = facing
        self.deadKids.add(newDeadKid)

    def addKid(self, x, y):
        newKid = Kid()
        newKid.animations = self.baseKid.animations
        newKid.sounds = self.baseKid.sounds
        newKid.setPosition(x,y)
        self.kids.add(newKid)
        self.kids._spritelist.sort(gameObjectPosCompare)

    def addRandomKid(self):
        x = random.randrange(800)
        y = random.randrange(600)
        self.addKid(x,y)
